/*
  Copyright 2007 Anders Båtstrand

      Licensed under the Apache License, Version 2.0 (the "License");
      you may not use this file except in compliance with the License.
      You may obtain a copy of the License at

          http://www.apache.org/licenses/LICENSE-2.0

      Unless required by applicable law or agreed to in writing, software
      distributed under the License is distributed on an "AS IS" BASIS,
      WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
      See the License for the specific language governing permissions and
      limitations under the License.

 */

package org.dyndns.anderlin.jmplayer.model;

import javax.persistence.*;
import java.util.Date;
import java.util.Set;

@Entity
@NamedQueries({
@NamedQuery(name = "artist_bysearch", query = "select a from Artist a where upper(a.surname) like :text order by a.surname"),
@NamedQuery(name = "artist_all", query = "select a from Artist a order by a.surname")
        })
public class Artist extends JmplayerObject {

    /**
     * The firtname of this artist. Ie Glenn
     */
    @Basic
    @Column(nullable = false)
    private String firstname;

    /**
     * The family name of this artist. Ie Gould
     */
    @Basic
    @Column(nullable = false)
    private String surname;

    /**
     * Date of birth
     */
    @Temporal(TemporalType.DATE)
    @Column(nullable = false)
    private Date birth;

    /**
     * Date of death, if any
     */
    @Temporal(TemporalType.DATE)
    @Column(nullable = true)
    private Date death;

    /**
     * Pieces this artist has composed. Remember to use children pieces when needed
     */
    @OneToMany(mappedBy = "composer", cascade = CascadeType.ALL)
    private Set<Piece> pieces;

    /**
     * Performances made by this artist
     */
    @OneToMany(mappedBy = "performer")
    private Set<Performance> perfomances;


    public String getFirstname() {
        return firstname;
    }

    public void setFirstname(String firstname) {
        this.firstname = firstname;
    }

    public String getSurname() {
        return surname;
    }

    public void setSurname(String surname) {
        this.surname = surname;
    }


    public Date getBirth() {
        return birth;
    }

    public void setBirth(Date birth) {
        this.birth = birth;
    }

    public Date getDeath() {
        return death;
    }

    public void setDeath(Date death) {
        this.death = death;
    }

    public Set<Performance> getPerfomances() {
        return perfomances;
    }

    public void setPerfomances(Set<Performance> perfomances) {
        this.perfomances = perfomances;
    }


    public Set<Piece> getPieces() {
        return pieces;
    }

    public void setPieces(Set<Piece> pieces) {
        this.pieces = pieces;
    }


    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;

        Artist artist = (Artist) o;

        if (birth != null ? !birth.equals(artist.birth) : artist.birth != null) return false;
        if (death != null ? !death.equals(artist.death) : artist.death != null) return false;
        if (firstname != null ? !firstname.equals(artist.firstname) : artist.firstname != null) return false;
        if (surname != null ? !surname.equals(artist.surname) : artist.surname != null) return false;

        return true;
    }

    public int hashCode() {
        int result;
        result = (firstname != null ? firstname.hashCode() : 0);
        result = 31 * result + (surname != null ? surname.hashCode() : 0);
        result = 31 * result + (birth != null ? birth.hashCode() : 0);
        result = 31 * result + (death != null ? death.hashCode() : 0);
        return result;
    }

    public String toString() {
        return getFirstname() + " " + getSurname() + " (" + getBirth() + "-" + getDeath() + ")";
    }
}
